cc.Class({
	extends: cc.Component,

	properties: {
		chess: cc.Prefab,
	},

	onLoad() {
		this.init();
		this.node.on("touchstart", this.chessPlay, this);
		let that = this;
		cc.vv.ServerCallBack.moveChess = function (chessPos) {
			chessPos.x = chessPos.x | 0;
			chessPos.y = chessPos.y | 0;
			let x = (chessPos.x / 40) | 0;
			let y = (chessPos.y / 40) | 0;
			// console.log(x, y);
			let pos = cc.v2(x * 40, y * 40);
			if (that.chessArr[Math.abs(y)][Math.abs(x)] === -1) {
				that.chessArr[Math.abs(y)][Math.abs(x)] = cc.vv.ChessColor;
				that.createChess(pos);
			}
		};
	},

	// start () {   },
	init() {
		this.chessArr = [];
		for (let i = 0; i < 15; i++) {
			this.chessArr[i] = [];
			for (let j = 0; j < 15; j++) {
				this.chessArr[i].push(-1);
			}
		}
	},

	//落子
	chessPlay(event) {
		if (cc.vv.playerColor !== cc.vv.turnColor) {
			return;
		}
		let chessPos = event.getLocation();
		chessPos = this.node.convertToNodeSpaceAR(chessPos);
		cc.vv.CMM.request_moveChess(data, (err, info) => {});
		// chessPos.x = chessPos.x | 0;
		// chessPos.y = chessPos.y | 0;
		// let x = (chessPos.x / 40) | 0;
		// let y = (chessPos.y / 40) | 0;
		// // console.log(x, y);
		// let pos = cc.v2(x * 40, y * 40);
		// if (this.chessArr[Math.abs(y)][Math.abs(x)] === -1) {
		//   this.chessArr[Math.abs(y)][Math.abs(x)] = cc.vv.ChessColor;
		//   this.createChess(pos);
		// }
	},
	createChess(pos) {
		// let chessprefab = cc.vv.warehouse.prefab["chess"];
		let chess = cc.instantiate(this.chess);
		chess.setPosition(pos);
		chess.gc("chess").init();
		this.node.children[0].addChild(chess);
		this.node.runAction(
			cc.sequence(
				cc.callFunc(() => {
					this.victory(pos);
				}, this),
				cc.callFunc(() => {
					if (cc.vv.ChessColor) {
						cc.vv.ChessColor = 0;
					} else {
						cc.vv.ChessColor = 1;
					}
				}, this)
			)
		);
	},
	//胜负
	victory(pos) {
		pos.x = Math.abs(pos.x / 40);
		pos.y = Math.abs(pos.y / 40);
		let row = 0;
		let line = 0;
		let left = 0;
		let right = 0;
		//行
		let x = pos.x - 4;
		let y = pos.y;
		// console.log(this.chessArr);
		if (pos.x - 4 < 0) {
			x = 0;
		}
		// console.log(this.chessArr[y]);
		for (let i = x; i < this.chessArr[y].length; i++) {
			if (this.chessArr[y][i] === cc.vv.ChessColor) {
				row++;
			} else if (row > 0) {
				break;
			}
		}
		//列
		x = pos.x;
		y = pos.y - 4;
		if (pos.y - 4 < 0) {
			y = 0;
		}
		for (let i = y; i < this.chessArr.length; i++) {
			if (this.chessArr[i][x] === cc.vv.ChessColor) {
				line++;
			} else if (line > 0) {
				break;
			}
		}
		//右斜
		// x = pos.y - 4;
		// y = pos.x - 4;
		// if (x < 0) {
		//   x = 0;
		// }
		// if (y < 0) {
		//   y = 0;
		// }
		// for (let i = 0; i < 10; i++) {
		//   if (
		//     x + i <= this.chessArr.length &&
		//     y + i <= this.chessArr[0].length &&
		//     this.chessArr[x + i][y + i] === cc.vv.ChessColor
		//   ) {
		//     right++;
		//   } else if (right > 0) {
		//     break;
		//   }
		// }

		//左斜
		//     x = pos.y + 4;
		//     y = pos.x - 4;
		//     if (x > this.chessArr[0].length) {
		//       x = this.chessArr[0].length;
		//     }
		//     if (y < 0) {
		//       y = 0;
		//     }
		//     for (let i = 0; i < 10; i++) {
		//       if (
		//         x - i >= 0 &&
		//         y + i <= this.chessArr[0].length &&
		//         this.chessArr[x - i][y + i] === cc.vv.ChessColor
		//       ) {
		//         left++;
		//       } else if (left > 0) {
		//         break;
		//       }
		//     }

		//胜利判断
		if (left >= 5 || right >= 5 || line >= 5 || row >= 5) {
			console.log("胜利");
		}
	},
	// update (dt) {},
});
